<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拖动div</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            margin: 20px 0 0 40px;
            width: 300px;
            height: 300px;
            position: absolute;
            border: 1px solid #ddd;
            border-radius: 6px;
        }
        .box .title{
            cursor:move;
            height: 40px;
            user-select: none;
            text-align: center;
            line-height: 40px;
            background-color: #999;
            border-bottom: 1px solid#ddd;
        }
        .box .content{
            width: 100%;
            box-sizing: border-box;
            padding: 10px;
        }
        #test{
            margin: 50px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="title">
            <span>对话框标题</span>

        </div>
        <div class="content">
            <span>对话框内容</span>
        </div>


    </div>
    
</body>
<script>
    let box = document.querySelector('.box');
    console.log(box);
    console.dir(box);
    
    let boxTitle = box .querySelector('.title');
    boxTitle .addEventListener('mousedown',e => {
      let x = e.pageX - box.offsetLeft;
      let y = e.pageY- box.offsetTop;
     //根据鼠标移动是的实时坐标，计算。更新BOX的坐标
      function fun (e2){
          box.style.left = e2.pageX - x +'px';
          box.style.top = e2.pageY - y +'px';
      }
      //为文档对象添加鼠标移动的事件监听
      document.addEventListener('mousemove',fun);
      //为文档对象添加鼠标弹起的事件监听
      document.addEventListener('mouseup',e3 =>{
        document.removeEventListener('mousemove',fun)
      });
       
   })

</script>
</html>